//----------------------------------------------------------------------------------------
// @Project Includes
#include "includes.h"

//----------------------------------------------------------------------------------------
// @Local Macro Define in the module

//----------------------------------------------------------------------------------------
// @Local Type defines in the module

//----------------------------------------------------------------------------------------
// @Global Variables define in the module

//----------------------------------------------------------------------------------------
// @Prototypes of functions in the module

//----------------------------------------------------------------------------------------
/**
 * @brief  This function handles TIM1 update interrupt request.
 */
void TIM1_UP_IRQHandler(void)
{
    if (TIM_GetIntStatus(TIM1, TIM_INT_UPDATE) != RESET)/*10ms*/
    {
        TIM_ClrIntPendingBit(TIM1, TIM_INT_UPDATE);
				drv_can_10msInt();
				app_task_timecount();
    }
}

/**
 * @brief  driver time sleep
 * @param  none
 * @return none
 * @note   none
 */
void drv_time_sleep(void)
{
    /* TIM1 enable update irq */
    TIM_ConfigInt(TIM1, TIM_INT_UPDATE, DISABLE);
    /* TIM1 enable counter */
    TIM_Enable(TIM1, DISABLE); 	
}

/**
 * @brief  driver time wakeup
 * @param  none
 * @return none
 * @note   none
 */
void drv_time_wakeup(void)
{
		drv_time_init();	
}

/**
 * @brief  driver time initialization
 * @param  none
 * @return none
 * @note   none
 */
void drv_time_init(void)
{
    NVIC_InitType NVIC_InitStructure;
    TIM_TimeBaseInitType TIM_TimeBaseStructure;
    uint16_t PrescalerValue = 0;   

    /* PCLK1 = HCLK/4 */
    //RCC_ConfigPclk2(RCC_HCLK_DIV4);
    /* TIM1 clock enable */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM1, ENABLE);
    
    /* Enable the TIM1 global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel                   = TIM1_UP_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 4;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);  
    /*time = (SystemCoreClock/(PrescalerValue+1))/TIM_TimeBaseStructure.Period*/
    /* Compute the prescaler value */
		#if SYSCLK_TYPE == SYSCLK_HSE_PLL
			PrescalerValue = 63; //(uint16_t) (SystemCoreClock / 12000000) - 1;
		#else
			PrescalerValue = 64; //(uint16_t) (SystemCoreClock / 12000000) - 1;
		#endif   
    /* Time base configuration */
    TIM_InitTimBaseStruct(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.Period    = 10000;//65535;
    TIM_TimeBaseStructure.Prescaler = 0;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;
    TIM_InitTimeBase(TIM1, &TIM_TimeBaseStructure);
    /* Prescaler configuration */
    TIM_ConfigPrescaler(TIM1, PrescalerValue, TIM_PSC_RELOAD_MODE_IMMEDIATE);
    /* TIM1 enable update irq */
    TIM_ConfigInt(TIM1, TIM_INT_UPDATE, ENABLE);
    /* TIM1 enable counter */
    TIM_Enable(TIM1, ENABLE);
}

// =======================================================================================
// End of file.
// =======================================================================================
